package com.threeigtache.threeigproject.mianwork;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.threeigtache.adpter.WalletActivityAdapter;
import com.threeigtache.been.WalletBeen;
import com.threeigtache.contect.UriContect;
import com.threeigtache.threeigproject.BasicActivity;
import com.threeigtache.threeigproject.CompontApplayCation;
import com.threeigtache.threeigproject.R;
import com.threeigtache.utils.GsonUtil;
import com.threeigtache.utils.ToastMake;
import com.threeigtache.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class WalletActivity extends BasicActivity {

    private TextView              mMoney;
    private TextView              mTakemoney;
    private RecyclerView          mRecycleview;
    private WalletActivityAdapter mAdapter;
    private List<WalletBeen.DataBean> mList = new ArrayList<>();
    private SmartRefreshLayout mRefreshLayout;
    private TextView           mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_wallet);
        setLeftShow(3, R.mipmap.message);
        //点击小人去登录界面
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mMoney = (TextView) findViewById(R.id.money);
        mText = (TextView) findViewById(R.id.text);
        mTakemoney = (TextView) findViewById(R.id.takemoney);
        mRecycleview = (RecyclerView) findViewById(R.id.recycleview);


        //下拉刷新
        mRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smartrefreshlayout);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(WalletActivity.this));
        //下拉刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                mRefreshLayout.finishRefresh();
            }
        });

        //上拉加载更多
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mRefreshLayout.finishLoadmore();
            }
        });

        //点击提取现金
        mTakemoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(WalletActivity.this);
        mRecycleview.setLayoutManager(manager);
        mAdapter = new WalletActivityAdapter(mList);
        mRecycleview.setAdapter(mAdapter);

        if (Utils.netConnected(WalletActivity.this)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    haveMoney();
                }
            }).start();

        }
    }

    private void haveMoney() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", CompontApplayCation.phone);
        String json = GsonUtil.parseMapToJson(map);
        mList.clear();
        showProgrees("");
        OkHttpUtils
                .postString()
                .url(UriContect.RECORD)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideProgree();
                        ToastMake.showToast("获取数据失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        final WalletBeen bean = GsonUtil.parseJsonToBean(response, WalletBeen.class);
                        if (bean==null){
                            ToastMake.showToast("获取数据失败");
                            return;
                        }
                        if (!TextUtils.equals(bean.getStatus(),"1")){
                            ToastMake.showToast(bean.getMsg());
                            return;
                        }else {
                            Utils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    mMoney.setText(bean.getSum());
                                    List<WalletBeen.DataBean> data = bean.getData();
                                    if(data.size()==0){
                                        mRecycleview.setVisibility(View.GONE);
                                        mText.setVisibility(View.VISIBLE);
                                    }else {
                                        mRecycleview.setVisibility(View.VISIBLE);
                                        mText.setVisibility(View.GONE);
                                        mList.addAll(data);
                                        mAdapter.notifyDataSetChanged();
                                    }

                                }
                            });
                        }

                    }
                });
    }
}
